<template>
  <el-dialog
    :title="formData.id ? '修改' : '添加'"
    :visible.sync="showDialog"
    width="50%"
    @close="closeDialog"
  >
    <!-- 内容 -->
    <el-form
      :model="formData"
      :rules="formRules"
      label-width="200px"
      ref="form"
    >
      <el-form-item label="角色名称" prop="name">
        <el-input v-model="formData.name"></el-input>
      </el-form-item>
      <el-form-item label="角色描述" prop="description">
        <el-input v-model="formData.description"></el-input>
      </el-form-item>
      <el-form-item label="权限节点">
        <el-tree
          :data="allMenuList"
          v-if="showDialog"
          show-checkbox
          :props="{ label: 'title' }"
          node-key="id"
          ref="tree"
        ></el-tree>
      </el-form-item>
    </el-form>
    <span slot="footer">
      <el-button @click="showDialog = false">取消</el-button>
      <el-button type="primary" @click="btnOK">确定</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { getAllMenuListApi } from '@/api/menu'
import { addRoleApi, getRoleInfoApi, editRoleApi } from '@/api/role'
export default {
  name: 'addRole',
  data() {
    return {
      showDialog: false,
      formData: {
        name: '',
        description: '',
        menuids: []
      },
      formRules: {
        name: [{ required: true, message: '请输入', trigger: 'blur' }],
        description: [{ required: true, message: '请输入', trigger: 'blur' }]
      },
      allMenuList: [],
      checkTreeId: [] // 叶子节点ID
    }
  },
  created() {
    this.initData()
  },
  methods: {
    async initData() {
      this.allMenuList = await getAllMenuListApi()
    },
    closeDialog() {
      this.formData = {
        name: '',
        description: '',
        menuids: []
      }
      this.$refs.form.clearValidate()
    },
    async btnOK() {
      try {
        await this.$refs.form.validate()
      } catch (error) {
        return console.log(error)
      }
      //   获取勾选的节点和半勾的节点 的ID
      //   获取对钩  this.$refs.tree.getCheckedKeys()
      //   获取半钩  this.$refs.tree.getHalfCheckedKeys()
      const checkId = this.$refs.tree.getCheckedKeys()
      const halfCheckId = this.$refs.tree.getHalfCheckedKeys()
      this.formData.menuids = [...checkId, ...halfCheckId]
      if (this.formData.id) {
        await editRoleApi(this.formData)
        this.$message.success('修改成功')
      } else {
        await addRoleApi(this.formData)
        this.$message.success('添加成功')
      }
      this.$emit('updateList')
      this.showDialog = false
    },
    // 编辑回显
    async getRowInfo(id) {
      this.formData = await getRoleInfoApi(id)
      this.showDialog = true
      // console.log(this.formData.authList)
      // 等到弹框打开之后，里面的组件内容都渲染好了之后再去回显树；否则树组件还不存在呢
      this.$nextTick(() => {
        // 如何回显树呢？
        // 思路： 获取到 authList 里面的 叶子节点，放到树上面打钩即可；
        this.getCheckId(this.formData.authList)
        // this.checkTreeId所有的叶子节点了
        this.$refs.tree.setCheckedKeys(this.checkTreeId)
      })
    },
    // 获取叶子节点ID
    getCheckId(arr) {
      arr.forEach((item) => {
        if (item.children && item.children.length) {
          // 说明还有儿子
          this.getCheckId(item.children)
        } else {
          // 说明是叶子
          this.checkTreeId.push(item.id)
        }
      })
    }
  }
}
</script>
<style scoped></style>
